const express = require('express');
const router = express.Router();
const Blog = require('../../mongoModuels/rear/blog');
const time  = require('./../../utils/time');
const jwt = require('jsonwebtoken');

router

.post('/write/article', (req, res) => {
    if(req.body.info && req.body.title) {
        let creationTime = time(new Date().getTime());
        let token = req.get("Authorization");
        let founderName = jwt.decode(token).username;
        let data = {
            title: req.body.title,
            info: req.body.info,
            name: founderName,
            creationTime: creationTime
        }
        if(req.body.id) {
            Blog.draft.updateOne({'_id': req.body.id}, data).then(() => {
                res.json({
                    status: 1,
                    message: '保存成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '保存失败'
                })
            })
        } else {
            Blog.draft.create(data, err => {
                if(err) {
                    res.json({
                        status: 0,
                        message: '保存失败'
                    })
                } else {
                    res.json({
                        status: 1,
                        message: '保存成功'
                    })
                }
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//上传文章至草稿箱

.get('/getdraft', (req, res) => {
    if(req.query.id) {
        Blog.draft.findOne({'_id': req.query.id}, (err, data) => {
            if(err) {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            } else {
                res.json({
                    status: 1,
                    message: '操作成功',
                    data
                })
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
// 根据id获取草稿箱文章

.get('/getidarticle', (req, res) => {
	if(req.query.id) {
		Blog.article.findOne({'_id': req.query.id}, (err, data) => {
			if(err) {
				res.json({
				    status: 0,
				    message: '操作失败'
				})
			} else {
				res.json({
				    status: 1,
				    message: '操作成功',
				    data
				})
			}
		})
	} else {
		res.json({
		    status: 0,
		    message: '参数不足'
		})
	}
})
//根据id获取文章

.get('/draft', (req, res) => {
    if(req.query.pageSize && req.query.pageNum) {
        let data = {
            pageSize: parseInt(req.query.pageSize),
            pageNum: parseInt(req.query.pageNum),
            keyWord: req.query.keyWord
        }
        let title = new RegExp(data.keyWord, 'i');
        let isKeyWord = {'$or': [{'title': title}]};
        Blog.draft.count(isKeyWord).then(count => {
            let query = Blog.draft.find(isKeyWord);
            query.skip((data.pageNum - 1) * data.pageSize).limit(data.pageSize);
            query.exec({}).then(rows => {
                res.json({
                    status: 1,
                    message: '操作成功',
                    data: {
                        rows,
                        count
                    }
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '操作失败'
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//查看草稿箱

.post('/delete/draft', (req, res) => {
    if(req.body.id) {
        Blog.draft.remove({'_id': req.body.id}).then(() => {
            res.json({
                status: 1,
                message: '删除成功'
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '删除失败'
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//删除草稿箱文章

.post('/release/article', (req, res) => {
    if(req.body.info && req.body.title && req.body.label && req.body.type) {
        let creationTime = time(new Date().getTime());
        let data = {
            info: req.body.info,
            title: req.body.title,
            label: req.body.label,
            type: 1,
            mag: [],
            creationTime: creationTime,
            mold: req.body.label.type
        }
        if(req.body.id) {
            Blog.draft.findOne({'_id': req.body.id}, (err, rows) => {
                if(err) {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                }else {
                    data.name = rows.name;
                    Blog.draft.remove({'_id': req.body.id}).then(() => {
                        Blog.article.create(data, err => {
                            if(err) {
                                res.json({
                                    status: 0,
                                    message: '操作失败'
                                })
                            } else {
                                res.json({
                                    status: 1,
                                    message: '发布成功'
                                })
                            }
                        })
                    }).catch(() => {
                        res.json({
                            status: 0,
                            message: '操作失败'
                        })
                    })
                }
            })
        } else {
            let token = req.get("Authorization");
            let founderName = jwt.decode(token).username;
            data.name = founderName;
            Blog.article.create(data, err => {
                if(err) {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                } else {
                    res.json({
                        status: 1,
                        message: '发布成功'
                    })
                }
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//发布文章

.post('/release/idarticle', (req, res) => {
	if(req.body.id && req.body.info && req.body.title && req.body.label && req.body.type) {
		let data = {
			info: req.body.info,
			title: req.body.title,
			label: req.body.label,
			mold: parseInt(req.body.type),
		}
		Blog.article.updateOne({'_id': req.body.id}, data, err => {
			if(err) {
				res.json({
				    status: 0,
				    message: '发布失败'
				})
			} else {
				res.json({
				    status: 1,
				    message: '发布成功',
					data
				})
			}
		})
	} else {
		res.json({
		    status: 0,
		    message: '参数不足'
		})
	}
})
//修改已发布的文章


module.exports = router;